﻿@using SelfMadeMillionaire.Core
@using SelfMadeMillionaire.Core.Enums
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.Formatters
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Newsletters
@using PagedList.Mvc
@model NewsletterListViewModel

<div class="table-responsive">
		@if (Model.Newsletters.Any())
  {
			<table class="table table-bordered tablesorter">
				<thead>
					<tr>
						<th><a href="#">Date</a></th>
						<th><a href="#">Issue Type</a></th>
						<th><a href="#">Symbols</a></th>
						<th><a href="#">Letter</a></th>
						<th><a href="#">Title</a></th>
                        <th><a href="#">Level</a></th>
						<th><a href="#">Sent On</a></th>
						<th>Action</th>
					</tr>
				</thead>
				<tbody>
					@foreach (var item in Model.Newsletters)
	 {
						<tr>
							<td>@item.Date.ToString("MM/dd/yyyy")</td>
							<td>@item.IssueType.Title</td>
							<td>@item.Symbols</td>
							<td>@ProductTypeFormatter.GetProductTypeName((ProductType)Enum.Parse(typeof(ProductType), item.Letter.ToString()))</td>
							<td><a href="@Url.Action("Details", "Newsletters", new { id = item.ID, area = "" })">@item.Title</a></td>	
                            <td>@Html.Raw(DateSentFormatter.FormatMinAccessLevel(item))</td>
							<td>@Html.Raw(DateSentFormatter.FormatSentDate(item.EmailMessage))</td>
							<td>
                                <a href="@FileLink.GetAbsoluteLinkOnFile(item.PdfFileName)" target="_blank">View</a>
                                | <a href="@Url.Action("Edit", new { id = item.ID })">Edit</a>	
                                @if (item.EmailMessage == null)
                                {
                                    <text>
                                    | <a href="javascript:void(0);"  onclick="if (confirm('Are you sure you want to delete this issue?')) { Delete(@(item.ID)) }">Delete</a>									
									| <a href="javascript:void(0);" onclick="if (confirm('Are you sure to send this newsletter?')) { Send(@(item.ID)) }">Send</a>
                                    | <a href="javascript:void(0);" onclick="if (confirm('Are you sure to send this newsletter?')) { SendTester(@(item.ID)) }">Send test</a>
								    </text>
                                }
                                else
                                {
                                    if (!item.IsHide)
                                    {
                                    <text>
                                    | <a href="javascript:void(0);"  onclick="if (confirm('Are you sure to hide this newsletter?')) { Hide(@(item.ID)) }">Hide</a>
                                    </text>
                                    }
                                    else
                                    {
                                    <text>
                                    | <a href="javascript:void(0);"  onclick="if (confirm('Are you sure to show this newsletter?')) { Show(@(item.ID)) }">Show</a>
                                    </text>
                                    }
                                }
							</td>
						</tr>
	 }
				</tbody>
			</table>
	  @Html.PagedListPager(Model.Newsletters, page => Url.Action("Index", new { page, startDate = Model.StartDate, endDate = Model.EndDate, selectedProduct = Model.ProductTypes.SelectedValue, issueType = Model.IssueTypes.SelectedValue, symbols = Model.Symbols,  title = Model.Title, minAccessLevel = Model.AccessLevels.SelectedValue, sort = Model.SortOrder }))
  }
  else
  {
			<p>No records found.</p>
  }
	</div>

<script type="text/javascript">
	$(function () {
        $("#newsletters-list table").tablesorter({
			headers: {
			    7: { sorter: false }
			}
		});

		var sortRefresh = true;
	    @if(!string.IsNullOrEmpty(Model.SortOrder))
        {
            <text>
	            sortRefresh = false;
	            setInitSort("newsletters-list", '@(Model.SortOrder)');
            </text>
        }

	    $("#newsletters-list table").on("sortEnd", function (event) {
	        $("#SortOrder").val(event.target.config.sortList);
	        if (sortRefresh)
	            GetList();
	        else
	            sortRefresh = true;

		    });
	});
</script>